<?php
namespace app\Common;

use PhalApi\Api;


/**
 * 钱包基础接口
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */

class WalletBase extends Api
{
    /**
     * 用户注册账户,生成uid
     * {"id":100019,"createdAt":1537519405427,"updatedAt":1537519405427,"type":"TRADER","level":0,"internal":false,"canSignin":true,"canTrade":true,"canWithdraw":true}
     */
    public static function userregister()
    {
        $params = '{"canSignin":"true","canTrade":"true","canWithdraw":"true"}';
        $header = \App\getHeader('UIInternalKey');
        $data = \App\curl(FWALLETURL . '/ui/users', 'POST', $params, $header);
        return json_decode($data);
    }

    /**
     * 使该地址支持查询这个币
     * string(141) "{"id":100025,"createdAt":1538207279241,"userId":100051,"bipIndex":17,"currency":"ETH","address":"0x300e995700c3f65defade94e1c16b9e2182e3d5e"}"
     */
    public static function deposit($userId=0, $ico)
    {
        $header = \App\getHeader('UIInternalKey');
        $data = \App\curl(FWALLETURL . '/ui/users/'.$userId.'/deposit/' .$ico. '/address', 'GET', [], $header);
        return json_decode($data);
    }

    /**
     * 查询余额
     * {"spotAccounts":[{"id":100052,"createdAt":1537520100919,"updatedAt":1537520611722,"userId":100019,"currency":"ETH","type":"SPOT_AVAILABLE","balance":1.010000000000000000}]}
     */
    public static function accounts($userId=0)
    {
        $header = \App\getHeader('UIInternalKey');
        $data = \App\curl(FWALLETURL . '/ui/users/'.$userId.'/accounts', 'GET', [], $header);
        return json_decode($data);
    }

    /**
     * 查询某个币的充值记录
     * @param
     * @
     */
    public static function depositLogs($userId = 0)
    {
        $header = \App\getHeader('UIInternalKey');
        //$userId = 100051;
        $params = ['currency' => 'BBYY', 'limit' => 100];
        $data = \App\curl(FWALLETURL . '/ui/users/'.$userId.'/deposit/logs', 'GET', $params, $header);
        return json_decode($data);
    }

    /*
     *  提币免审核 放行交易 外部交易
     *  {"result":true}
     */
    public static function check($txid)
    {
        $header = \App\getHeader('ManageInternalKey');
        $params = '{"success":"true","needReview":"false","errorCode":"0","errorMessage":"成功"}';
        $data = \App\curl(FWALLETURL. '/manage/withdraws/' . $txid . '/check', 'POST', $params, $header);
        return json_decode($data);
    }
    /**
     * 获取转账用的地址id
     *
     */
    public static function withdraw($userid, $ico, $address)
    {
        $header = \App\getHeader('UIInternalKey');
        $params = '{"address":"'.$address.'","currency":"'.$ico.'","description":""}';
        $data = \App\curl(FWALLETURL . '/ui/users/'.$userid.'/withdraw/addresses', 'POST', $params, $header);
        return json_decode($data);
    }

    /**
     * 内部交易 不收取gas费
     */

    public function transfer($amount, $ico, $fromuid, $touid, $flowType = 'TRADE_FREEZE')
    {
        $header = \App\getHeader('ManageInternalKey');
        $params = '{"amount":"'.$amount.'","currency":"'.$ico.'","description":"","flowType":"'.$flowType.'","fromUserId":"'.$fromuid.'","toUserId":"'.$touid.'"}';
        $data = \App\curl(FWALLETURL . '/manage/accounts/transfer', 'POST', $params, $header);
        return json_decode($data);
    }

    /**
     * 转账交易  外部交易  收取gas费
     * string(274) "{"id":100013,"createdAt":1537521313403,"updatedAt":1537521313403,"status":"SUBMITTED","errorCode":"","errorMessage":"","currency":"ETH","userId":100019,"toAddress":"0xAB91eD6Ca9961D52e4F0962FbD9Aef06Bc6F1Ce0","amount":0.1,"fee":0.0050000000000000,"tx":"","cancellable":true}"
     */
    public static function withdrawRequest($amount, $ico = '',$userId = 100051, $addressid)
    {
        $header = \App\getHeader('UIInternalKey');
        $params = '{"amount":"'.$amount.'","currency":"'.$ico.'","withdrawAddressId":"'.$addressid.'"}';
        $data = \App\curl(FWALLETURL . '/ui/users/'.$userId.'/withdraw/requests', 'POST', $params, $header);
        return json_decode($data);
    }

    /**
     * 提币规则,  每个币都需设置, gas费
     *
     */
    public static function withdrawRules($ico, $feeRate, $maximumFee, $minimumAmount, $minimumFee)
    {
        $header = \App\getHeader('ManageInternalKey');
        $params = '{"feeRate":"'.$feeRate.'","maximumFee":"'.$maximumFee.'","minimumAmount":"'.$minimumAmount.'","minimumFee":"'.$minimumFee.'","withdrawDisabled": false}';
        $data = \App\curl(FWALLETURL. '/manage/'.$ico. '/withdraw/rules', 'POST', $params, $header);
        return json_decode($data);
    }


    /***
     *  发币系列接口   第一步 发合约
     *  @params
     *  @name 发币名
     *  @address 发币地址
     *  @num 发币数量
     *  @decimal 小数点
     *  @accessToken 签名
     *  @privateKey 发币钱包的私钥
     */
    public function fundIssue($name, $address = '0xc66cd3deec506713d653681c7663f2c4fe96fb3f', $num = 100000000, $decimal = 9, $privateKey = '0x605297a0025b7b6bff5b7d219e78cd4f7c4dce928e86069d64c99a47547aa1e9', $accessToken = '43b3bc2abe306f06dc8bf5081f8d1f14aa97f79c704a7f148337600ca01ce41b')
    {
        $params = '{
            "params": ["'. $name .'","'. $address .'",'. $num .','. $decimal .',"UNIT"],
            "accessToken":"'. $accessToken .'",
            "sender": "'. $privateKey .'",
            "id": "fund_create_001"
        }';
        $data = \App\curl(WALLETURL . '/fund/issue', 'POST', $params, ['Content-Type: application/json']);
        return json_decode($data);
    }

    /***
     *
     *  发币系列接口    第二步 事务收据
     * @params
     * @hash 第一步生成的hash值
     * @accessToken 签名
     */
    public function transactionReceipt($hash = '0xf946d98a93f1dcb8363a68fe8b40b986e983bf73e052321f7088381a83e3ecba', $accessToken = '43b3bc2abe306f06dc8bf5081f8d1f14aa97f79c704a7f148337600ca01ce41b')
    {
        $params = '{
            "hash":"'. $hash .'",
            "accessToken":"'. $accessToken .'",
            "id": "transaction_receipt_003"
        }';
        $data = \App\curl(WALLETURL . '/transaction/receipt', 'POST', $params, ['Content-Type: application/json']);
        return json_decode($data);
    }

    /***
     *  发币系列接口    第三步 保存信息
     * @params
     * @name 发币名称
     * @accessToken 签名
     * @contractAddress 第二步生成的合约地址 contractAddress
     */
    public function fundSave($name, $contractAddress, $accessToken = '43b3bc2abe306f06dc8bf5081f8d1f14aa97f79c704a7f148337600ca01ce41b')
    {
        $params = '{
            "name":"'. $name .'",
	        "address":"'. $contractAddress .'",
	        "accessToken":"'. $accessToken .'",
	        "id": "fund_save_001"
        }';
        $data = \App\curl(WALLETURL . '/fund/save', 'POST', $params, ['Content-Type: application/json']);
        return json_decode($data);
    }

    /***
     *  发币系列接口    第四步  转账
     * @params
     * @name 发币名称
     * @num 转账数量
     * @accessToken 签名
     * @address 转账地址
     * @secretKey 钱包地址对应的私钥
     */
    public static function fundTransfer($name, $address = '0x300e995700c3f65defade94e1c16b9e2182e3d5e', $num, $secretKey, $accessToken = '43b3bc2abe306f06dc8bf5081f8d1f14aa97f79c704a7f148337600ca01ce41b')
    {
        $params = '{
            "name":"'. $name .'",
            "params": ["'. $address .'", '. $num .'],
            "sender": "'. $secretKey .'",
            "accessToken":"'. $accessToken .'",
            "id": "fund_transfer_001"
        }';
        $data = \App\curl(WALLETURL . '/fund/transfer', 'POST', $params, ['Content-Type: application/json']);
        return json_decode($data);
    }

    /**
     *  第五部  使该币可以交易 wallet服务支持发的token  以转账至公司帐号
     */
    public static function currencies($name, $tokenContractAddress, $tokenDecimals)
    {
        $header = \App\getHeader('ManageInternalKey');
        $params = '{"name":"' . $name . '","legal":false,"token":true,"addressAliasTo":"ETH","tokenIssuesOn":"ETH","xpubKey":"","tokenContractAddress":"' . strtolower($tokenContractAddress) . '","tokenDecimals":"' . $tokenDecimals .'"}';
        $data = \App\curl(FWALLETURL . '/manage/currencies', 'POST', $params, $header);
        return json_decode($data);
    }
}